package io.ticktick.modules.app.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.ticktick.modules.app.annotation.Login;
import io.ticktick.modules.app.entity.UserAccountEntity;
import io.ticktick.modules.app.service.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.ticktick.modules.app.entity.FriendsEntity;
import io.ticktick.modules.app.service.FriendsService;
import io.ticktick.common.utils.R;

import javax.websocket.server.PathParam;


/**
 * 好友列表
 *
 * @author hwf
 * @email zhongku0225@gmail.com
 * @date 2019-11-13 17:15:19
 */
@RestController
@RequestMapping("app/friends")
public class FriendsController {
    @Autowired
    private FriendsService friendsService;
    @Autowired
    private UserAccountService userAccountService;

    /**
     * 列表
     */
    @Login
    @GetMapping("/list/{userId}")
    public R list(@PathVariable("userId") Long userId){
        List<FriendsEntity> list = friendsService.list(new QueryWrapper<FriendsEntity>().eq("user_id", userId));

        for (FriendsEntity friendsEntity : list) {
            friendsEntity.setUserAccountEntity(userAccountService.getOne(new QueryWrapper<UserAccountEntity>().eq("user_id", friendsEntity.getFriendId())));
        }

        return R.ok().put("friends", list);
    }

    @Login
    @GetMapping("/liststatus/{id}")
    public R listWithStatus(@PathVariable("id") Long userId, @RequestParam("status") Integer status){
        List<FriendsEntity> list = friendsService.list(new QueryWrapper<FriendsEntity>().eq("user_id", userId).eq("status", status));

        return R.ok().put("friends", list);
    }


    /**
     * 信息
     */
//    @Login
//    @GetMapping("/info/{id}")
//    public R info(@PathVariable("id") Long id){
//		FriendsEntity friends = friendsService.getById(id);
//
//        return R.ok().put("friends", friends);
//    }

    @Login
    @GetMapping("/info/{userId}")
    public R infoByFriendId(@PathVariable("userId") Long userId, @RequestParam("friendId") Long friendId){
        FriendsEntity friends = friendsService.getOne(new QueryWrapper<FriendsEntity>().eq("user_id", userId).eq("friend_id", friendId));
        friends.setUserAccountEntity(userAccountService.getById(friendId));
        return R.ok().put("friends", friends);
    }

    /**
     * 保存
     */
    @Login
    @PostMapping("/save")
    public R save(@RequestBody FriendsEntity friends){
		friendsService.save(friends);

        return R.ok();
    }

    /**
     * 修改
     */
    @Login
    @RequestMapping("/update")
    public R update(@RequestBody FriendsEntity friends){
		friendsService.updateById(friends);

        return R.ok("修改成功");
    }

    /**
     * 删除
     */
    @Login
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] userIds){
		friendsService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
